import requests
from lxml import etree
import csv

from mess import info

for i in range(1,11):
    url = 'https://www.guazi.com/suqian/buy/o%s/#bread' % i
    # print(url)

    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
        'Cookie':'uuid=c1093acc-e20e-450a-d853-7ef5671fd085; clueSourceCode=%2A%2300; user_city_id=292; ganji_uuid=2059193228752431872463; sessionid=15b079ed-4546-4992-9549-117311586e11; lg=1; Hm_lvt_bf3ee5b290ce731c7a4ce7a617256354=1605140094; close_finance_popup=2020-11-12; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22seo_baidu%22%2C%22ca_n%22%3A%22default%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%22c1093acc-e20e-450a-d853-7ef5671fd085%22%2C%22ca_city%22%3A%22suqian%22%2C%22sessionid%22%3A%2215b079ed-4546-4992-9549-117311586e11%22%7D; _gl_tracker=%7B%22ca_source%22%3A%22-%22%2C%22ca_name%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_id%22%3A%22-%22%2C%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22sid%22%3A32813234522%7D; cityDomain=suqian; antipas=5813k04996868r26953e94CRa0; preTime=%7B%22last%22%3A1605144593%2C%22this%22%3A1605140091%2C%22pre%22%3A1605140091%7D; Hm_lpvt_bf3ee5b290ce731c7a4ce7a617256354=1605144592'
    }

    r = requests.get(url, headers=headers, timeout=3)
    print('爬取的网页：', r.url)
    r.encoding = 'utf-8'
    html = etree.HTML(r.text)
    # print(r.text)
    li_list = html.xpath('//li/a')
    print(len(li_list))
    for li in li_list:
        item = dict()
        item['车辆简介'] = li.xpath('./@title')
        item['上市时间'] = li.xpath('./div[@class="t-i"][1]/text()[1]')
        item['使用寿命'] = li.xpath('./div[@class="t-i"][1]/text()[2]')
        item['原价'] = li.xpath('./div[@class="t-price"]/em/text()')
        item['现价'] = li.xpath('./div[@class="t-price"]/p/text()[1]')
        # print(item['现价'])
        # if len(item.get('车辆简介')) > 0:
        #     # print('1')
        #     item = str(item)
        #
        #     with open(r'./mess.py','a+',encoding='utf-8') as f:
        #         f.write(item + ',' + '\n')
        with open('test.csv', 'w')as f:
            f_csv = csv.DictWriter(f,['车辆简介', '上市时间', '使用寿命', '原价', '现价'])
            f_csv.writeheader()
            f_csv.writerows(info)